﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApp.CommonSupport.Filter;
using DoNet.Common.Helper;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Services;
using MvcApp.Models;
using MvcApp.CommonSupport;
using System.Linq.Expressions;

namespace MvcApp.Controllers
{
    public class PostController : Controller
    {
        private readonly IPostService _postService;

        public PostController(IPostService postService)
        {
            _postService = postService;
        }

        public JsonResult PostJsonList(string organizeId)
        {
            GridData<Post> gd = new GridData<Post>();
            if (organizeId != null)
            {
                ISpecification<Post> baseCondition = new DirectSpecification<Post>(p => p.IsDeleted == false);
                ISpecification<Post> condition = new DirectSpecification<Post>(p => p.OrganizeId == new Guid(organizeId));
                condition = new AndSpecification<Post>(condition, baseCondition);
                Expression<Func<Post, string>> orderByExpression = p => p.Code;
                bool IsDESC = false;
                List<Post> postData = _postService.GetList<string>(condition, orderByExpression, IsDESC);
                gd.total = postData.Count();
                gd.rows = postData;
            }
            else
            {
                gd.total = 0;
                gd.rows = new List<Post>();
            }
            return Json(gd);
        }

        public ActionResult InsertOrEditPost(Post post)
        {
            CurrentUser currentUser = new CurrentUser();
            post.SysCreatedBy = currentUser.UserInfo.Id;
            post.SysCreatedOn = DateTime.Now;
            post.SysModifiedBy = currentUser.UserInfo.Id;
            post.SysModifiedOn = DateTime.Now;
            string rtn = _postService.InsertOrEditPost(post);
            return Content(rtn);
        }

        public ActionResult DeletePostById(string id)
        {
            _postService.DeletePostById(new Guid(id));
            return Content("删除成功！");
        }
    }
}
